package com.xyzh.application.config;


import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;

import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.converts.PostgreSqlTypeConvert;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.querys.PostgreSqlQuery;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import com.baomidou.mybatisplus.generator.keywords.PostgreSqlKeyWordsHandler;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;


/**
 * @author JZB
 * @date 2020/6/23 17:07
 */
public class postgresqlGenerator {
    public static void main(String[] args) {
        FastAutoGenerator.create(new DataSourceConfig
                .Builder("jdbc:postgresql://82.156.151.136:5432/cfal", "stj", "stj2024@cfal")
                .dbQuery(new PostgreSqlQuery())
                .schema("public")
                .keyWordsHandler(new PostgreSqlKeyWordsHandler())
                .typeConvert(new PostgreSqlTypeConvert()))
                .globalConfig(builder ->
                                builder.author("@JZB") // 设置作者
                                        .enableSwagger() // 开启 swagger 模式
                                        .disableOpenDir()

                                        .dateType(DateType.ONLY_DATE)
//                  .fileOverride() // 覆盖已生成文件
                                        .outputDir(System.getProperty("user.dir") + "/xyzh-stj/src/main/java") // 指定输出目录
                ).packageConfig(builder ->
                builder.parent("com.xyzh.stj.web") // 设置父包名
                        .entity("entity")
                        .service("service")
                        .serviceImpl("service.impl")
                        .mapper("dao")
                        .xml(null)
                        .controller("controller") // 设置父包模块名
                        .pathInfo(Collections.singletonMap(OutputFile.xml, System.getProperty("user.dir") + "/xyzh-stj/src/main/resources/mybatis-mappers/")) // 设置mapperXml生成路径

        ).strategyConfig((scanner, builder) -> builder.addInclude(Arrays.asList("objects_attr","objects_eav","objects_ercv","objects_id","objects_val","view_entity_attrs"))
                .enableCapitalMode()
                .entityBuilder().enableLombok().naming(NamingStrategy.underline_to_camel).columnNaming(NamingStrategy.underline_to_camel).superClass("com.xyzh.application.base.entity.BasicEntity")
//                .addSuperEntityColumns(new String[]{"createby", "createtime", "updatetime", "updateby"})
                .idType(IdType.NONE).formatFileName("%sEntity").fileOverride()

                .controllerBuilder().enableRestStyle().superClass("com.xyzh.application.base.controller.BasicController")

                .serviceBuilder().superServiceImplClass("com.xyzh.application.base.service.BasicServiceImpl")
                .serviceBuilder().superServiceClass("com.xyzh.application.base.service.BasicService").formatServiceFileName("%sService")

                .mapperBuilder().enableBaseResultMap().enableBaseColumnList().superClass("com.xyzh.application.base.dao.BasicMapper")
                .fileOverride()
                .convertXmlFileName(entityName ->
                        entityName.replace("Entity", "") + "Mapper").fileOverride()

                .build()
        ).templateEngine(new FreemarkerTemplateEngine())
                .execute();

    }
}
